package gov.mmgt.netmanager.operate;

import gov.mmgt.netmanager.common.Constant;
import gov.mmgt.netmanager.common.exception.MyException;
import gov.mmgt.netmanager.common.util.ConfigurationFile;
import gov.mmgt.netmanager.vo.ParameterVO;

import org.apache.log4j.Logger;


/**
 * 
 * Copyright (c) 2009,niux<br>
 * All rights reserved.<br>
 * 
 * 文件名称：PropertiesOperate.java<br>
 * 摘    要：属性操作类<br>
 * <br>
 * 当前版本：<br>
 * 作    者：铭显 Email: <a href="mailto:liangmingxian@gmail.com">liangmingxian@gmail.com</a><br>
 * 完成日期：@since 2014-5-17<br>
 * <br>
 * 取代版本：<br>
 * 原作者  ：<br>
 * 完成日期：@since Oct 22, 2009<br>
 */
public class PropertiesOperate {
	private Logger log = Logger.getLogger(getClass());
	
	private String filePath;
	
	public PropertiesOperate(String filePath) {
		super();
		this.filePath = filePath;
	}
	
	/**
	 * 
	 * 方法概述：读取系统参数
	 * 描述：读取系统参数
	 * @throws MyException
	 * 返回类型：void
	 * 修改人：梁铭显 
	 * 修改时间：2014-5-26
	 */
	@SuppressWarnings("static-access")
	public void getParameter() throws MyException {
		log.debug("读取配置文件");
		
		try{
			ConfigurationFile config = new ConfigurationFile();
			Constant.PARAMETER.setVersion(config.getProfileString(this.filePath, "Version", "Version", "")); //版本号
			Constant.PARAMETER.setDataSource(config.getProfileString(this.filePath, "Config", "DataSource", "")); //数据源路径
			Constant.PARAMETER.setAdapter(config.getProfileString(this.filePath, "Config", "Adapter", "")); //网卡
			Constant.PARAMETER.setAutoRun(config.getProfileString(this.filePath, "Config", "AutoRun", "")); //开机运行
			Constant.PARAMETER.setAutoStart(config.getProfileString(this.filePath, "Config", "AutoStart", "")); //自动开始
			Constant.PARAMETER.setAutoAddToBlackList(config.getProfileString(this.filePath, "Config", "AutoAddToBlackList", "")); //自动添加黑名单
			Constant.PARAMETER.setInterval(config.getProfileString(this.filePath, "Config", "Interval", "")); //扫描时间间隔
		} catch (Exception e) {
			MyException myException = new MyException("读取系统参数失败！错误代码：1008");
			throw myException;
		}
	}
	
	/**
	 * 
	 * 方法概述：保存系统参数
	 * 描述：保存系统参数
	 * @param parameter
	 * @throws MyException
	 * 返回类型：void
	 * 修改人：梁铭显 
	 * 修改时间：2014-5-26
	 */
	@SuppressWarnings("static-access")
	public void saveParameter(ParameterVO parameter) throws MyException {
		log.debug("保存配置文件");
		
		try {
			ConfigurationFile config = new ConfigurationFile();
			config.setProfileString(this.filePath, "Version", "Version", parameter.getVersion());
			config.setProfileString(this.filePath, "Config", "DataSource", parameter.getDataSource());
			config.setProfileString(this.filePath, "Config", "Adapter", parameter.getAdapter());
			config.setProfileString(this.filePath, "Config", "AutoRun", parameter.getAutoRun());
			config.setProfileString(this.filePath, "Config", "AutoStart", parameter.getAutoStart());
			config.setProfileString(this.filePath, "Config", "AutoAddToBlackList", parameter.getAutoAddToBlackList());
			config.setProfileString(this.filePath, "Config", "Interval", parameter.getInterval());
		} catch (Exception e) {
			MyException myException = new MyException("保存系统参数失败！错误代码：1009");
			throw myException;
		}
	}
}
